package Error::Common;
use Error;
use base qw(Exporter);
use strict;
use warnings;
our $VERSION = 0.01;
our @EXPORT = qw(assert_param_refs);

$Carp::Internal{Error}++;
$Error::Debug = 1;

{ package Error::Fatal;
  use base 'Error';
}
{ package Error::UndefRef;
  use base 'Error';
}
{ package Error::IllegalArgument;
  use base 'Error';
  use Lingua::EN::Inflect qw(NUMWORDS ORD);
  use overload '""' => 'stringify';
  sub stringify { my( $this ) = @_;
		  "Expected $this->{-expected}, got $this->{-got}"
		    .' as ' . NUMWORDS(ORD($this->{-number})) . ' argument'
		      . " of $this->{-function}";
		}
  sub throw { my $exception = shift @_;
	      my %h_ = @_;
	      my %h = ( -function => (caller(1))[3] );
	      %h = ( %h, %h_ );
	      local $Error::Depth = $Error::Depth + 2;
	      $exception->SUPER::throw( %h,
					-text => stringify(\%h),
				      );
	    }
}
{ package Error::AccessDenied;
  use base 'Error';
}
{ package Error::Math;
  use base 'Error';
}

sub assert_param_refs ($$) {
  my( $refs_arrayref, $reftypes_arrayref ) = @_;

  Error::IllegalArgument->throw( -number   => 1,
				 -expected => 'ARRAY',
				 -got      => ref $refs_arrayref,
			       )
      unless 'ARRAY' eq ref $refs_arrayref;

  Error::IllegalArgument->throw( -number   => 2,
				 -expected => 'ARRAY',
				 -got      => ref $reftypes_arrayref,
			       )
      unless 'ARRAY' eq ref $reftypes_arrayref;

  local $Error::Depth = $Error::Depth + 1;
  my $f = (caller(1))[3];
  my $i = 0;
  my @original = @$refs_arrayref;
  for my $type ( @$reftypes_arrayref ) {
    my $ref  = shift @$refs_arrayref;
    $i++;

    Error::IllegalArgument->throw( -function => $f,
				   -number   => $i,
				   -expected => $type,
				   -got      => ref $ref,
				 )
	unless $type eq ref $ref;
  }

  return( @original );
}

1;
__END__
#################### main pod documentation begin ###################
## Below is the stub of documentation for your module. 
## You better edit it!


=head1 NAME

Error::Common - Common exceptions framework.

=head1 SYNOPSIS

  use Error::Common;
  blah blah blah


=head1 DESCRIPTION

Stub documentation for this module was created by ExtUtils::ModuleMaker.
It looks like the author of the extension was negligent enough
to leave the stub unedited.

Blah blah blah.


=head1 USAGE



=head1 BUGS



=head1 SUPPORT



=head1 AUTHOR

    Eugene Grigoriev
    CPAN ID: SIZUR
    eugene.grigoriev@gmail.com
    http://search.cpan.org/~sizur

=head1 COPYRIGHT

This program is free software licensed under the...

	The BSD License

The full text of the license can be found in the
LICENSE file included with this module.


=head1 SEE ALSO

perl(1).

=cut

#################### main pod documentation end ###################

